package com.adgox.tiantianbiting.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.adgox.tiantianbiting.R;
import com.adgox.tiantianbiting.bean.ProgramChooseBean;
import com.bumptech.glide.Glide;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by tang on 2018/12/26.
 */

public class ProgramChooseAdapter extends RecyclerView.Adapter<ProgramChooseAdapter.ViewHolder> implements View.OnClickListener {
    private List<ProgramChooseBean> datas;
    private int programType;
    private int clickPostion = -1;

    public ProgramChooseAdapter(List<ProgramChooseBean> datas, int programType) {
        this.datas = datas;
        this.programType = programType;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_program_choose, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ProgramChooseBean bean = datas.get(position);
        Glide.with(holder.itemView.getContext()).load(bean.getImageUrl()).into(holder.img);
        holder.tvName.setText("第"+(position+1)+"集 "+bean.getTitle());
        holder.tvIntro.setText(bean.getDescribe());
        if (position == clickPostion){
            holder.tvName.setTextColor(holder.itemView.getContext().getResources().getColor(R.color.goldtext));
        }

        //免费节目
         //不做任何处理


        //仅支持全集购买节目
        if (programType == 2) {
            //可试听
            if (bean.getTryStatus() == 0) {
                holder.tvPrice.setVisibility(View.VISIBLE);
                holder.tvPrice.setText("免费听");
                holder.img_lock.setVisibility(View.INVISIBLE);
            } else {
                //不可试听
                if (bean.getBuySelf() == 0) {
                    //未购买
                    holder.img_lock.setVisibility(View.VISIBLE);
                } else {
                    //已购买
                    holder.img_lock.setVisibility(View.INVISIBLE);
                }
            }
        } else

        //支持分集购买节目
        if (programType == 3) {
            //免费集
            if (bean.getTryStatus() == 0){
                holder.tvPrice.setText("免费听");
                holder.tvPrice.setVisibility(View.VISIBLE);
                holder.img_lock.setVisibility(View.INVISIBLE);
            } else {
                //非免费集
                if (bean.getBuySelf() == 0){
                    //未购买
                    holder.tvPrice.setText(bean.getPrice()+"听币");
                    holder.tvPrice.setVisibility(View.VISIBLE);
                    holder.img_lock.setVisibility(View.VISIBLE);
                } else {
                    //已购买
                    holder.tvPrice.setVisibility(View.INVISIBLE);
                    holder.img_lock.setVisibility(View.INVISIBLE);
                }
            }
        }

        holder.itemView.setTag(position);
    }

    //加载更多刷新数据
    public void addDatas(List<ProgramChooseBean> list) {
        datas.addAll(list);
    }

    //被点击栏变色
    public void setClickPostion(int clickPostion){
        this.clickPostion = clickPostion;
    }


    @Override
    public int getItemCount() {
        return datas.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img)
        ImageView img;
        @BindView(R.id.img_lock)
        ImageView img_lock;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_intro)
        TextView tvIntro;
        @BindView(R.id.tv_price)
        TextView tvPrice;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            itemView.setOnClickListener(ProgramChooseAdapter.this);
        }
    }

    public interface onItemClickListener {
        void onItemClick(View view, int position);
    }

    public onItemClickListener listener = null;

    public void setOnItemClickListener(onItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View view) {
        int position = (int) view.getTag();
        listener.onItemClick(view, position);
    }

    public void clear(){
        datas.clear();
        notifyDataSetChanged();
    }
}
